from django.http import HttpResponse
from django.shortcuts import render
from django.views import View
from blog.models import Blog, Category, Tag, Comment
from django.core.paginator import Paginator, PageNotAnInteger
import markdown
from blog.forms import CommentForm


# 主页
class Index(View):
    def get(self, request):
        result = Blog.objects.get_queryset().order_by('-id')
        # 日志的数量
        blog_count = Blog.objects.count()
        # 分类数量
        category_count = Category.objects.count()
        # 标签的数量
        tag_count = Tag.objects.count()

        # 分页器
        # 创建分类器,每页最大为2
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1

        p = Paginator(result, 3)
        result = p.page(page)
        context = {
            'result': result,
            'blog_count': blog_count,
            'category_count': category_count,
            'tag_count': tag_count,

        }
        return render(request, 'index.html', context)


# 文章详情
class Blog_detail(View):
    def get(self, request, blog_id):
        # 顯示評論
        comment = Comment.objects.all()
        # 一共多少评论
        comment_nums = Comment.objects.count()
        blog_id = int(blog_id)
        blog_detail = Blog.objects.filter(id=blog_id)
        for blog in blog_detail:
            blog.content = markdown.markdown(blog.content)
        # 前一篇文章
        blog_previous = Blog.objects.filter(id=blog_id + 1)

        # 下一篇文章

        blog_next = Blog.objects.filter(id=blog_id - 1)

        context = {
            'blog_detail': blog_detail,
            'blog_previous': blog_previous,
            'blog_next': blog_next,
            'comment': comment,
            'comment_nums': comment_nums
        }
        return render(request, 'blog-detail.html', context)


# 标签页
class Tag_detail(View):
    def get(self, request):
        result = Tag.objects.all()
        # 日志的数量
        blog_count = Blog.objects.count()
        # 分类数量
        category_count = Category.objects.count()
        # 标签的数量
        tag_count = Tag.objects.count()
        context = {
            'result': result,
            'blog_count': blog_count,
            'category_count': category_count,
            'tag_count': tag_count
        }
        return render(request, 'tags.html', context)


# 文章页
class Archive(View):
    def get(self, request):
        result = Blog.objects.all().order_by('-create_time')
        # 日志的数量
        blog_count = Blog.objects.count()
        # 分类数量
        category_count = Category.objects.count()
        # 标签的数量
        tag_count = Tag.objects.count()

        # 分页器
        # 创建分类器,每页最大为2
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1

        p = Paginator(result, 7)
        result = p.page(page)

        context = {
            'result': result,
            'blog_count': blog_count,
            'category_count': category_count,
            'tag_count': tag_count,
        }
        return render(request, 'archive.html', context)


# 添加评论
class AddComment(View):
    def post(self, request):

        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            comment_form.save()
            return HttpResponse('{"status": "success"}', content_type='application/json')
        else:
            return HttpResponse('{"status": "fail"}', content_type='application/json')


# 标签显示博客
class TagDetailBlog(View):
    def get(self, request, tag_name):
        tag = Tag.objects.filter(name=tag_name).first()
        tag_blogs = tag.blog_set.all()

        # 分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1

        p = Paginator(tag_blogs, 5)
        tag_blogs = p.page(page)
        return render(request, 'tag-detail.html', {
            'tag_blogs': tag_blogs,
            'tag_name': tag_name,

        })
